package lib.book;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.util.ArrayList;
import java.util.List;
import lib.book.bookDao;
import lib.book.bookModule;

/**
 * Servlet implementation class bookServlet
 */
@WebServlet("/bookServlet")
public class bookServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public bookServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		String bno=request.getParameter("bno");
		String title=request.getParameter("title");
		String category=request.getParameter("category");
		String author=request.getParameter("author");
		String year_str=request.getParameter("year");
		String price_str=request.getParameter("price");
		
		int year=0;
		if(year_str!="") {
			year=Integer.parseInt(year_str);
		}
		double price=0.0;
		if(price_str!="") {
			price=Double.parseDouble(price_str);
		}
		bookDao bd= new bookDao();
		if(bno!="") {
			List<bookModule> res=bd.searchBookByBno(bno);
			request.setAttribute("res", res);
		}
		else if(title!="") {
			List<bookModule> res = bd.searchBookByTitle(title);
			for(int i=0;i<res.size();i++) {
			System.out.println(res.get(i).getBno());
		}
			request.setAttribute("res", res);
		}
		else if(author!="") {
			List<bookModule> res=bd.searchBookByAuthor(author);
			request.setAttribute("res", res);
		}
		else if(category!="") {
			List<bookModule> res=bd.searchBookByCategory(category);
			request.setAttribute("res", res);
		}
		else if(year_str!=""){
			List<bookModule> res=bd.searchBookByYear(year);
			request.setAttribute("res", res);
			
		}
		else if(price_str!=""){
			List<bookModule> res=bd.searchBookByPrice(price);
			request.setAttribute("res", res);
		}
		else {
			List<bookModule> res=bd.searchBookByTitle("");
			request.setAttribute("res", res);
		}
//		if(request.getSession().getAttribute("operate_state")==null) {
//			request.getSession().setAttribute("operate_state", -1);
//		}
		request.getRequestDispatcher("show_book.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
